﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ZKLT25.API.Helper;
using ZKLT25.API.IServices;
using ZKLT25.API.IServices.Dtos;

namespace ZKLT25.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class AskController : ControllerBase
    {
        private readonly IAskService _service;

        public AskController(IAskService service)
        {
            _service = service;
        }

        #region  阀体型号维护
        /// <summary>
        /// 分页查询阀体型号列表
        /// </summary>
        /// <param name="qto">查询条件</param>
        /// <returns></returns>
        [HttpGet("GetPagedList")]
        public async Task<ResultModel<PaginationList<Ask_FTListDto>>> GetPagedListAsync([FromQuery] Ask_FTListQto qto)
        {
            var res = await _service.GetFTPagedListAsync(qto);
            if (res.Data != null)
                Response.Headers.Append("TotalCount", res.Data.TotalCount.ToString());
            return res;
        }

        /// <summary>
        /// 创建阀体型号
        /// </summary>
        /// <param name="cto">创建请求</param>
        /// <returns></returns>
        [HttpPost("CreateFT")]
        public async Task<ResultModel<bool>> CreateFTAsync([FromBody] Ask_FTListCto cto)
        {
            return await _service.CreateFTAsync(cto);
        }

        /// <summary>
        /// 更新阀体型号
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <param name="uto">更新请求</param>
        /// <returns></returns>
        [HttpPut("{id}/UpdateFT")]
        public async Task<ResultModel<bool>> UpdateFTAsync([FromRoute] int id, [FromBody] Ask_FTListUto uto)
        {
            return await _service.UpdateFTAsync(id, uto);
        }

        /// <summary>
        /// 删除阀体型号
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        [HttpDelete("{id}/DeleteFT")]
        public async Task<ResultModel<bool>> DeleteFTAsync([FromRoute] int id)
        {
            return await _service.DeleteFTAsync(id);
        }

        /// <summary>
        /// 检查阀体型号是否存在
        /// </summary>
        /// <param name="ftVersion">阀体型号</param>
        /// <param name="excludeId">排除的ID</param>
        /// <returns></returns>
        [HttpGet("CheckFTExists")]
        public async Task<ResultModel<bool>> CheckFTExistsAsync([FromQuery] string ftVersion, [FromQuery] int? excludeId = null)
        {
            return await _service.ExistsFTAsync(ftVersion, excludeId);
        }

        /// <summary>
        /// 批量更新系数
        /// </summary>
        [HttpPost("BatchUpdateFTRatio")]
        public async Task<ResultModel<bool>> BatchUpdateFTRatioAsync([FromBody] BatchUpdateRatioRequest request)
        {
            return await _service.BatchUpdateFTRatioAsync(request.Ids, request.Ratio);
        }
        #endregion

        #region  附件控制器
        /// <summary>
        /// 分页查询附件列表
        /// </summary>
        /// <param name="qto">查询条件</param>
        /// <returns></returns>
        [HttpGet("GetFJPagedList")]
        public async Task<ResultModel<PaginationList<Ask_FJListDto>>> GetFJPagedListAsync([FromQuery] Ask_FJListQto qto)
        {
            var res = await _service.GetFJPagedListAsync(qto);
            if (res.Data != null)
                Response.Headers.Append("TotalCount", res.Data.TotalCount.ToString());
            return res;
        }

        /// <summary>
        /// 批量更新系数
        /// </summary>
        [HttpPost("BatchUpdateFJRatio")]
        public async Task<ResultModel<bool>> BatchUpdateFJRatioAsync([FromBody] BatchUpdateRatioRequest request)
        {
            return await _service.BatchUpdateFJRatioAsync(request.Ids, request.Ratio);
        }
        #endregion


        #region 阀体 / 附件日志控制器
        /// <summary>
        /// 分页查询阀体 / 附件日志
        /// </summary>
        /// <param name="qto">查询条件（MainID 和 DataType 必传）</param>
        /// <returns>分页日志列表</returns>
        [HttpGet("GetFTFJLogPagedList")]
        public async Task<ResultModel<PaginationList<Ask_FTFJListLogDto>>> GetFTFJLogPagedListAsync([FromQuery] Ask_FTFJListLogQto qto)
        {
            var res = await _service.GetFTFJLogPagedListAsync(qto);
            if (res.Data != null)
                Response.Headers.Append("TotalCount", res.Data.TotalCount.ToString());
            return res;
        }
        #endregion
    }
}
